//
//  HMSelectApplyGoodTableViewCell.m
//  HMMarket
//
//  Created by haohao on 2017/6/26.
//  Copyright © 2017年 homai. All rights reserved.
//

#import "HMSelectApplyGoodTableViewCell.h"
#import "ChooseGoodView.h"
#import "ChooseGoodModel.h"
@interface HMSelectApplyGoodTableViewCell ()

NS_STRONG ChooseGoodView *chooseGoodView;

NS_ASSIGN float totalNumber;

NS_STRONG ChooseGoodModel * model;

@end

@implementation HMSelectApplyGoodTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    _nameLabel.textColor = kc9c9c9;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    [_selectBtn addTarget:self action:@selector(selectGood) forControlEvents:UIControlEventTouchUpInside];
    // Initialization code
}

- (void)selectGood {
    [self.chooseGoodView show];
    [[UIApplication sharedApplication].keyWindow endEditing:NO];
    self.chooseGoodView.oid = _id;
    DefaultWeakSelf;
    _totalNumber = 0;
    self.chooseGoodView.actionBlock = ^(NSMutableArray *selectedArray) {
        NSString *nameString;
        if (selectedArray.count == 0) {
            weakSelf.nameLabel.text = @"请选择退货商品";
            if ([weakSelf.delegate respondsToSelector:@selector(returnGoodPrice:goodArray:)]) {
                [weakSelf.delegate returnGoodPrice:weakSelf.totalNumber goodArray:selectedArray];
            }
        } else {
            for (int i = 0; i < selectedArray.count; i++) {
                ChooseGoodModel *model = selectedArray[i];
                if (nameString.length == 0) {
                    nameString = model.name;
                } else {
                    nameString = [NSString stringWithFormat:@"%@,%@", [HMReductionData replaceNullValue:nameString], model.name];
                }
                _totalNumber = [model.price floatValue] * [model.item_number floatValue] + _totalNumber;
            }
            if ([weakSelf.delegate respondsToSelector:@selector(returnGoodPrice:goodArray:)]) {
                [weakSelf.delegate returnGoodPrice:weakSelf.totalNumber goodArray:selectedArray];
            }
            weakSelf.nameLabel.text = nameString;
        }
    };
    
}

- (void)setGoodArray:(NSArray *)goodArray {
    _goodArray = goodArray;
    NSString *nameString;
    for (int i = 0; i < _goodArray.count; i++) {
        ChooseGoodModel *model = _goodArray[i];
        if (nameString.length == 0) {
            nameString = model.name;
        } else {
            nameString = [NSString stringWithFormat:@"%@,%@", [HMReductionData replaceNullValue:nameString], model.name];
        }
    }
    _nameLabel.text = nameString;
}


- (ChooseGoodView *)chooseGoodView {
    if (_chooseGoodView == nil) {
        _chooseGoodView = [[ChooseGoodView alloc] initWithFrame:CGRectMake(0, 0, KWidthSCREEN, KHeightSCREEN)];
    }
    return _chooseGoodView;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
